package com.atguigu.springcloud.service.impl;

import com.atguigu.springcloud.dao.OrderDao;
import com.atguigu.springcloud.domain.Order;
import com.atguigu.springcloud.service.AccountService;
import com.atguigu.springcloud.service.OrderService;
import com.atguigu.springcloud.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.security.AlgorithmConstraints;

@Component
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderDao orderDao;

    @Resource
    private StorageService storageService;

    @Resource
    private AccountService accountService;

    @Override
    @GlobalTransactional(name = "my_test_tx_group",rollbackFor = Exception.class)
    public void create(Order order) {

        log.info("========>下单开始");
        orderDao.create(order);

        storageService.decrease(order.getProductId(), order.getCount());

        accountService.decrease(order.getUserId(), order.getMoney());

        log.info("order Service 开始 修改订单状态");
        orderDao.update(order.getUserId(), 0);
        log.info("order Service  修改订单状态结束");

        log.info("===========>下单结束！");

    }
}
